import axios from '@/utils/request.js'
import { getStorage } from '@/utils/storage.js'

// 获取登录接口
// ({ mobile, code })  传入一个对象格式的参数
export const loginAPI = ({ mobile, code }) =>
  axios({
    url: '/v1_0/authorizations',
    method: 'POST',
    data: {
      mobile,
      code
    }
  })

// 获取所有 频道
export const getAllChannelsAPI = () =>
  axios({
    url: '/v1_0/channels',
    method: 'GET' // 要是请求的接口文档默认的请求方式是GET，这个 method 是可写可不写
  })

// 获取 - 用户选择的频道
// 注意：用户没有登录，默认返回后台设置的默认频道
export const getUserChannelsAPI = () =>
  axios({
    url: '/v1_0/user/channels'
  })

// 获取文章列表数据
export const getArticleListAPI = ({ channelId, timestamp }) =>
  axios({
    url: '/v1_0/articles',
    params: {
      channel_id: channelId,
      timestamp: timestamp || new Date().getTime()
    }
  })

// 反馈不感兴趣
export const articleDislikeAPI = artId =>
  axios({
    url: '/v1_0/article/dislikes',
    method: 'POST',
    data: {
      target: artId
    }
  })

// 文章 - 举报
export const articleReportAPI = ({ target, type, remark }) =>
  axios({
    url: '/v1_0/article/reports',
    method: 'POST',
    data: {
      target,
      type,
      remark
    }
  })

// 文章 - 详情
export const articlesDetailAPI = ({ article_id }) =>
  axios({
    url: `/v1_0/articles/${article_id}`
  })

// 文章 - 关注作者
export const followedUserAPI = ({ target }) =>
  axios({
    url: '/v1_0/user/followings',
    method: 'POST',
    data: {
      target: target
    }
  })

// 文章 - 取消关注作者
export const unFollowedUserAPI = ({ uid }) =>
  axios({
    // 这uid只是个变量名, 把值拼接在url发给后台(无需指定参数名)
    url: `/v1_0/user/followings/${uid}`,
    method: 'DELETE'
  })

// 文章 - 点赞
export const articleLikeAPI = ({ artId }) =>
  axios({
    url: '/v1_0/article/likings',
    method: 'POST',
    data: {
      target: artId
    }
  })

// 文章 - 取消点赞
export const articleDisLikeAPI = ({ artId }) =>
  axios({
    url: `/v1_0/article/likings/${artId}`,
    method: 'DELETE'
  })

// 评论 - 获取列表
// offset第一页时, 不用在params携带(axios发现值为null会自动忽略此参数)
export const commentListAPI = ({ artId, offset = null, limit = 10 }) =>
  axios({
    url: '/v1_0/comments',
    params: {
      type: 'a',
      source: artId,
      offset,
      limit
    }
  })

// 评论 - 发布
export const sendCommentAPI = ({ artId, content }) =>
  axios({
    url: '/v1_0/comments',
    method: 'POST',
    data: {
      target: artId,
      content: content
    }
  })

// 更新频道

export const updateChannelAPI = ({ channels }) =>
  axios({
    url: '/v1_0/user/channels',
    method: 'PUT',
    data: {
      channels
    }
  })

// 删除频道
export const deleteChannelAPI = channelId =>
  axios({
    url: `/v1_0/user/channels/${channelId}`,
    method: 'DELETE'
  })

// 搜索 - 获取联想建议
export const suggestListAPI = ({ keywords }) =>
  axios({
    url: '/v1_0/suggestion',
    params: {
      q: keywords
    }
  })
// 搜索结果列表
export const searchResultAPI = ({ page = 1, per_page = 10, q }) =>
  axios({
    url: '/v1_0/search',
    params: {
      page,
      per_page,
      q
    }
  })

// 用户 - 基本资料
export const userInfoAPI = () =>
  axios({
    url: '/v1_0/user'
  })

// 用户- 个人资料(就为了获取生日)
export const userProfileAPI = () =>
  axios({
    url: '/v1_0/user/profile'
  })

// 用户- 更新头像
// 注意: formObj的值必须是一个表单对象
// '{"a": 10, "b": 20}' // 对象格式的JSON字符串
// new FormData() // 表单对象
export const updatePhotoAPI = formObj =>
  axios({
    url: '/v1_0/user/photo',
    method: 'PATCH',
    data: formObj
    // 如果你的请求体内容是表单对象, 浏览器会自动携带请求头Content-Type为multipart/form-data
  })

// 用户 - 更新资料
export const updateProfileAPI = ({ birthday, userName }) =>
  axios({
    url: '/v1_0/user/profile',
    method: 'PATCH',
    data: {
      birthday: birthday,
      name: userName
    }
  })

// 用户 - 更新token
export const refreshTokenAPI = () =>
  axios({
    url: '/v1_0/authorizations',
    method: 'PUT',
    headers: {
      Authorization: `Bearer ${getStorage('refresh_token')}`
    }
  })
